﻿using Abp.Dependency;
using Abp.Events.Bus.Handlers;
using shenghua.Order.Orders.EventDatas;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.Member.Coupons
{
    class MemberCouponEventHandler: 
        //IEventHandler<AfterGenerateOrderEventData>, 
        IEventHandler<AfterCancelOrderEventData>, 
        IEventHandler<AfterFinishOrderEventData>,
        ITransientDependency
    {
        private readonly MemberCouponManager _memberCouponManager;

        public MemberCouponEventHandler(
            MemberCouponManager memberCouponManager
            )
        {
            _memberCouponManager = memberCouponManager;
        }

        //public void HandleEvent(AfterGenerateOrderEventData eventData)
        //{
        //    if(eventData.Order.Info.CouponId != null)
        //    {
        //        //_memberCouponManager.OccupyCoupon(eventData.Order.MemberInfo.Id, (int)eventData.Order.Info.CouponId).Wait();
        //    }
        //}

        public void HandleEvent(AfterCancelOrderEventData eventData)
        {
            if (eventData.Order.Info.CouponId != null)
            {
                _memberCouponManager.RelieveCoupon(eventData.Order.MemberInfo.Id, (int)eventData.Order.Info.CouponId).Wait();
            }
        }

        public void HandleEvent(AfterFinishOrderEventData eventData)
        {
            if (eventData.Order.Info.CouponId != null)
            {
                _memberCouponManager.UseCoupon(eventData.Order.MemberInfo.Id, (int)eventData.Order.Info.CouponId).Wait();
            }
        }
    }
}
